﻿namespace com.stifel.BranchMgmtDao
{
    using System;
    using System.IO;

    using com.stifel.Extensions.ByteExtensions;
    using com.stifel.Extensions.FileStreamExtensions;
    using com.stifel.Extensions.Int64Extensions;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.Extensions.StreamExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public partial class StifelPhoto : IBranchRelated
    {
        #region Methods

        public static StifelPhoto Get( SPListItem spListItem )
        {
            StifelPhoto o = new StifelPhoto( );
            o.Process( spListItem );
            return o;
        }

        public void Process( SPListItem spListItem )
        {
            SPLookupValue branchLookup = spListItem.GetValue<SPLookupValue>( Fields.Value.BranchLookupId );
            SPLookupValue photoType = spListItem.GetValue<SPLookupValue>( Fields.Value.PhotoTypeId );
            SPLookupValue itemTypeFSType = spListItem.GetValue<SPLookupValue>( Fields.Value.ItemTypeFSType );
            SPLookupValue itemType = spListItem.GetValue<SPLookupValue>( Fields.Value.ItemTypeId );
            SPLookupValue urlPath = spListItem.GetValue<SPLookupValue>( Fields.Value.URLPathId );

            Author = spListItem.GetValue<SPUserInfoExpanded>( Fields.Value.Author );
            BranchLookupId = branchLookup == null ? new Nullable<int>( ) : branchLookup.LookupId;
            BranchLookupTitle = branchLookup == null ? string.Empty : branchLookup.LookupValue;
            DatePictureTaken = spListItem.GetValue<DateTime?>( Fields.Value.DatePictureTaken );
            DocumentCreatedBy = spListItem.GetValue<string>( Fields.Value.DocumentCreatedBy );
            DocumentModifiedBy = spListItem.GetValue<string>( Fields.Value.DocumentModifiedBy );
            Comments = spListItem.GetValue<string>( Fields.Value.Comments );
            Copyright = spListItem.GetValue<string>( Fields.Value.Copyright );
            Created = spListItem.GetValue<DateTime>( Fields.Value.Created );
            Editor = spListItem.GetValue<SPUserInfoExpanded>( Fields.Value.Editor );
            EncodedAbsoluteURL = spListItem.GetValue<string>( Fields.Value.EncodedAbsoluteURL );
            FileType = spListItem.GetValue<string>( Fields.Value.FileType );
            HTMLFileType = spListItem.GetValue<string>( Fields.Value.HTMLFileType );
            Id = spListItem.ID;
            ItemTypeFSType = itemTypeFSType == null ? string.Empty : itemTypeFSType.ToString( );
            ItemTypeId = itemType == null ? new Nullable<int>( ) : itemType.LookupId;
            string displayUrl = spListItem.ParentList.Forms[ PAGETYPE.PAGE_DISPLAYFORM ].Url;
            ItemUrl = string.Format( "{0}/{1}?ID={2}", WebUrl, displayUrl, spListItem.ID );
            Keywords = spListItem.GetValue<string>( Fields.Value.Keywords );
            Modified = spListItem.GetValue<DateTime>( Fields.Value.Modified );
            Name = spListItem.GetValue<string>( Fields.Value.Name );
            ParentListId = spListItem.ParentList.ID;
            PhotoTypeId = photoType == null ? new Nullable<int>( ) : photoType.LookupId;
            PhotoTypeTitle = photoType == null ? string.Empty : photoType.LookupValue;
            Preview = spListItem.GetValue<bool>( Fields.Value.Preview ).ToString( );
            PreviewExists = spListItem.GetValue<bool>( Fields.Value.PreviewExists );
            PrimaryAuthor = spListItem.GetValue<string>( Fields.Value.PrimaryAuthor );
            Select = spListItem.GetValue<string>( Fields.Value.Select );
            ThumbnailExists = spListItem.GetValue<bool>( Fields.Value.ThumbnailExists );
            ThumbnailPreview = spListItem.GetValue<string>( Fields.Value.ThumbnailPreview );
            ThumbnailURL = spListItem.GetValue<string>( Fields.Value.ThumbnailURL );
            Title = spListItem.GetValue<string>( Fields.Value.Title );
            UniqueId = spListItem.UniqueId;
            URLPathFullUrl = spListItem.GetValue<string>( Fields.Value.URLPathFullUrl );
            URLPathId = urlPath == null ? new Nullable<int>( ) : urlPath.LookupId;
            WebImageURL = spListItem.GetValue<string>( Fields.Value.WebImageURL );
            WebUrl = spListItem.Web.Url;
        }

        public SPListItem XSet( SPListItem spListItem, StifelPhoto value )
        {
            if ( value.BranchLookupId.HasValue )
                spListItem.SetValue<SPLookupValue>( Fields.Value.BranchLookupId, new SPLookupValue( value.BranchLookupId.Value, value.BranchLookupTitle ) );
            if ( DatePictureTaken.HasValue )
                spListItem.SetValue<DateTime>( Fields.Value.DatePictureTaken, value.DatePictureTaken.Value );
            spListItem.SetValue<string>( Fields.Value.DocumentCreatedBy, value.DocumentCreatedBy );
            spListItem.SetValue<string>( Fields.Value.DocumentModifiedBy, value.DocumentModifiedBy );
            spListItem.SetValue<string>( Fields.Value.Comments, value.Comments );
            spListItem.SetValue<string>( Fields.Value.Copyright, value.Copyright );
            spListItem.SetValue<string>( Fields.Value.Keywords, value.Keywords );
            spListItem.SetValue<string>( Fields.Value.PrimaryAuthor, value.PrimaryAuthor );
            spListItem.SetValue<string>( Fields.Value.Title, value.Title );

            //using ( SPWeb spWeb = spListItem.Web )
            //{
            //    FileInfo f = new FileInfo( value.URLPathFullUrl );
            //    Stream fs = f.OpenRead( );
            //    byte[] buffer = new byte[0];
            //    fs.ToArray( );
            //    fs.Read( buffer, 0, fs.Length );
            //    FileStream stream = new FileStream( value.URLPathFullUrl, FileAccess.Read );
            //}

            return spListItem;
        }

        #endregion Methods

        #region Nested Types

        public class Fields
        {
            #region Fields

            static Fields _Value;

            #endregion Fields

            #region Constructors

            Fields( )
            {
                StifelPhoto p = new StifelPhoto( );
                Author = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.internalAuthor ) );
                BranchLookupId = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.BranchLookupId ) );
                Comments = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.Comments ) );
                ContentTypeId = "ContentTypeId";
                Copyright = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.Copyright ) );
                Created = Item.CREATED;
                DatePictureTaken = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.DatePictureTaken ) );
                DocumentCreatedBy = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.DocumentCreatedBy ) );
                DocumentModifiedBy = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.DocumentModifiedBy ) );
                Editor = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.internalEditor ) );
                EncodedAbsoluteURL = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.EncodedAbsoluteURL ) );
                FileType = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.FileType ) );
                HTMLFileType = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.HTMLFileType ) );
                ItemTypeFSType = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.ItemTypeFSType ) );
                ItemTypeId = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.ItemTypeId ) );
                Keywords = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.Keywords ) );
                Modified = Item.MODIFIED;
                Name = "FileLeafRef";
                PhotoTypeId = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.PhotoTypeId ) );
                Preview = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.PreviewExists ) );
                PreviewExists = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.PreviewExists ) );
                PrimaryAuthor = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.PrimaryAuthor ) );
                Select = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.Select ) );
                ThumbnailExists = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.ThumbnailExists ) );
                ThumbnailPreview = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.ThumbnailPreview ) );
                ThumbnailURL = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.ThumbnailURL ) );
                Title = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.Title ) );
                URLPathFullUrl = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.URLPathFullUrl ) );
                URLPathId = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.URLPathId ) );
                WebImageURL = Item.GetColumnName<StifelPhoto>( p.GetPropertySymbol( o => o.WebImageURL ) );
            }

            #endregion Constructors

            #region Properties

            public static Fields Value
            {
                get
                {
                    if ( _Value == null )
                        _Value = new Fields( );
                    return _Value;
                }
            }

            public string Author
            {
                get;
                private set;
            }

            public string BranchLookupId
            {
                get;
                private set;
            }

            public string Comments
            {
                get;
                private set;
            }

            public string ContentTypeId
            {
                get;
                private set;
            }

            public string Copyright
            {
                get;
                private set;
            }

            public string Created
            {
                get;
                private set;
            }

            public string DatePictureTaken
            {
                get;
                private set;
            }

            public string DocumentCreatedBy
            {
                get;
                private set;
            }

            public string DocumentModifiedBy
            {
                get;
                private set;
            }

            public string Editor
            {
                get;
                private set;
            }

            public string EncodedAbsoluteURL
            {
                get;
                private set;
            }

            public string FileType
            {
                get;
                private set;
            }

            public string HTMLFileType
            {
                get;
                private set;
            }

            public string ItemTypeFSType
            {
                get;
                private set;
            }

            public string ItemTypeId
            {
                get;
                private set;
            }

            public string Keywords
            {
                get;
                private set;
            }

            public string Modified
            {
                get;
                private set;
            }

            public string Name
            {
                get;
                private set;
            }

            public string PhotoTypeId
            {
                get;
                private set;
            }

            public string Preview
            {
                get;
                private set;
            }

            public string PreviewExists
            {
                get;
                private set;
            }

            public string PrimaryAuthor
            {
                get;
                private set;
            }

            public string Select
            {
                get;
                private set;
            }

            public string ThumbnailExists
            {
                get;
                private set;
            }

            public string ThumbnailPreview
            {
                get;
                private set;
            }

            public string ThumbnailURL
            {
                get;
                private set;
            }

            public string Title
            {
                get;
                private set;
            }

            public string URLPathFullUrl
            {
                get;
                private set;
            }

            public string URLPathId
            {
                get;
                private set;
            }

            public string WebImageURL
            {
                get;
                private set;
            }

            #endregion Properties
        }

        #endregion Nested Types
    }
}